<?php

function captiveregistration_title($Q,$dblayer,$params) {

	$T = captiveregistration_setup_custom_icons($Q);
  	$Q['icons']['PHPcaptive'] = load_icon_map($T);
	$titleImageSrc = "";
	if (isset($Q['icons']['PHPcaptive']['custom']['large']['PHPcaptive'])) {
		$titleImageSrc = $Q['base_url']."/".$T['icons']['location']."/".$Q['icons']['PHPcaptive']['custom']['large']['PHPcaptive'];
	} else {
		$titleImageSrc = system_get_var($dblayer,"default_cpanel_icon");
	}
	$title = "<h1><img valign='middle' src='{$titleImageSrc}' />&nbsp;Network Registration</h1>\n";
	
	return $title;
}
function captiveregistration_setup_custom_icons($Q) {
	$T = array();
  $T{'absolute_path'} = $Q{'absolute_path'};
  $T{'icons'} = array("label" => "captiveregistration", "location" => "site/modules/captiveregistration");
  return $T;
}
function captiveregistration_network_eligibility($Q,$dblayer,$providers,$params=null) {
	// go by user source.
	$network_id = null;
	$modref=null;
	if (isset($params['modref'])) {
		$modref = $params['modref'];
	}
	$userinfo = current_user($dblayer);
	//echo "<p>user: ".$userinfo['realname']."</p>\n";
	$user_groups = get_user_groups($dblayer,$userinfo['id']);
	//print_r($user_groups);
	if (count($user_groups)) {
		//just use first group (hopefully there should only be one)
		$netGroup = $dblayer->query("PHPcaptive",sprintf("select * from network_group_members where gid = %d LIMIT 1",$user_groups[0]));
		if ($netGroup->num_rows) {
			$net = $netGroup->fetch_object();
			$network_id = $net->network_id;
		} else {
			$network_id = null;
		}
	}
	return $network_id;
}
function captiveregistration_is_registered($dblayer,$addr) {
$registered = false;
	$result = $dblayer->query("PHPcaptiveI",sprintf("select * from registrations where hwaddr = '%s'",$addr));
	if ($result->num_rows) {
		$registered = true;
	}
return $registered;
}
function captiveregistration_network_to_id($dblayer,$network) {
	$id = null;
		$net = $dblayer->query("PHPcaptiveI",sprintf("select network_id from networks where label = '%s'",$network));
		if ($net->num_rows) {
			$row = $net->fetch_object();
			$id = $row->network_id;
		}
	return $id;
}
function captiveregistration_render_field($dblayer,$field,$params=null) {
$html = "";
$data = "";
	if (isset($field->type)) {
		switch ($field->type) {
			case "text":
				//if (isset($field->title) && isset($field->field_variable) && isset($field->description)) {
					if (isset($params['data'][$field->field_variable])) {
						$data = $params['data'][$field->field_variable];
					}
					if (isset($params['error'][$field->field_variable])) {
						$html .= "<p class=\"error\">\n";
					} else {
						$html .= "<p>\n";
					}
					$html .= "<strong>".$field->title."</strong>&nbsp;<input type=\"text\" name=\"".$field->field_variable."\" id=\"".$field->field_variable."\" value=\"{$data}\" />\n";
					if (isset($params['error'][$field->field_variable])) {
						$html .= "<br /><strong>".$params['error'][$field->field_variable]."</strong>\n";
					} else {
						if (isset($field->description)) {
							if ($field->description) {
								$html .= "<br /><em>".$field->description."</em>\n";
							}
						}
					}
					$html .= "</p>\n";
				//} else {
					//invalid title/field_variable/description
				//}
			break;
			case "date":
				//if (isset($field->title) && isset($field->field_variable) && isset($field->description)) {
					$day = date("d");
					if (isset($params['data'][$field->field_variable]['day'])) {
						$day = $params['data'][$field->field_variable]['day'];
					}
					$month = date("m");
					if (isset($params['data'][$field->field_variable]['month'])) {
						$month = $params['data'][$field->field_variable]['month'];
					}
					$year = date("Y");
					if (isset($params['data'][$field->field_variable]['year'])) {
						$year = $params['data'][$field->field_variable]['year'];
					}
					if (isset($params['error'][$field->field_variable])) {
						$html .= "<p class=\"error\">\n";
					} else {
						$html .= "<p>\n";
					}
					$html .= "<strong>".$field->title."</strong>&nbsp;<select name=\"".$field->field_variable."_day\" id=\"".$field->field_variable."_day\">\n";
						//generate days 1-31
						$i = 1;
						do {
							$SELECTED = "";
							if ($i == $day) {
								$SELECTED = " SELECTED ";
							}
							$html .= "<option value=\"{$i}\"{$SELECTED}>{$i}</option>\n";
						$i++;
						} while ($i <= 31);
					$html .= "</select>\n";
					$html .= "<select name=\"".$field->field_variable."_month\" id=\"".$field->field_variable."_month\">\n";
						//generate months 1-12
						$i = 1;
						do {
							$SELECTED = "";
							if ($i == $month) {
								$SELECTED = " SELECTED ";
							}
							$html .= "<option value=\"{$i}\"{$SELECTED}>{$i}</option>\n";
						$i++;
						} while ($i <= 12);
					$html .= "</select>\n";
					$html .= "<select name=\"".$field->field_variable."_year\" id=\"".$field->field_variable."_year\">\n";
						//generate years
						$i = (date("Y")-10);
						do {
							$SELECTED = "";
							if ($i == $year) {
								$SELECTED = " SELECTED ";
							}
							$html .= "<option value=\"{$i}\"{$SELECTED}>{$i}</option>\n";
						$i++;
						} while ($i <= (date("Y")+10));
					$html .= "</select>\n";
					if (isset($params['error'][$field->field_variable])) {
						$html .= "<br /><strong>".$params['error'][$field->field_variable]."</strong>\n";
					} else {
						if (isset($field->description)) {
							if ($field->description) {
								$html .= "<br /><em>".$field->description."</em>\n";
							}
						}
					}
					$html .= "</p>\n";
				//} else {
					//invalid title/field_variable/description
				//}
			break;
			case "checkbox":
				//if (isset($field->title) && isset($field->field_variable) && isset($field->description)) {
					$CHECKED = "";
					if (isset($params['data'][$field->field_variable])) {
						if ($params['data'][$field->field_variable] == "on") {/* TODO: What Value will be passed? */
							$CHECKED = " CHECKED ";
						}
					}
					if (isset($params['error'][$field->field_variable])) {
						$html .= "<p class=\"error\">\n";
					} else {
						$html .= "<p>\n";
					}
					$html .= "<input type=\"checkbox\" name=\"".$field->field_variable."\" id=\"".$field->field_variable."\"{$CHECKED}/>".$field->title."\n";
					if (isset($params['error'][$field->field_variable])) {
						$html .= "<br /><strong>".$params['error'][$field->field_variable]."</strong>\n";
					} else {
						if (isset($field->description)) {
							if ($field->description) {
								$html .= "<br /><em>".$field->description."</em>\n";
							}
						}
					}
					$html .= "</p>\n";
				//} else {
					//invalid title/field_variable/description
				//}
			break;
			case "textarea":
				//if (isset($field->title) && isset($field->field_variable) && isset($field->description)) {
					$DATA = "";
					if (isset($params['data'][$field->field_variable])) {
						$DATA = $params['data'][$field->field_variable];
					}
					if (isset($params['error'][$field->field_variable])) {
						$html .= "<p class=\"error\">\n";
					} else {
						$html .= "<p>\n";
					}
					$html .= "<strong>".$field->title."</strong><br />\n";
					if (isset($params['error'][$field->field_variable])) {
						$html .= "<br /><strong>".$params['error'][$field->field_variable]."</strong>\n";
					} else {
						if (isset($field->description)) {
							if ($field->description) {
								$html .= "<br /><em>".$field->description."</em>\n";
							}
						}
					}
					$html .= "<textarea name=\"".$field->field_variable."\" id=\"".$field->field_variable."\">\n";
						$html .= "{$DATA}\n";
					$html .= "</textarea>\n";
					$html .= "</p>\n";
				//} else {
					//invalid title/field_variable/description
				//}
			break;
			case "select":
			
			break;
			case "fieldset":
				//if (isset($field->title) && isset($field->field_variable) && isset($field->description)) {
					$html .= "<fieldset name=\"\" id=\"\">\n";
						$html .= "<legend>".$field->title."</legend>\n";
						if ($field->description) {
							$html .= "<p>".$field->description."</p>\n";
						}
				//} else {
					//invalid title/field_variable/description
				//}
			break;
			case "fieldset-end":
				$html .= "</fieldset>\n";
			break;
		}
	} else {
		//field type not set
		$html .= "<p>Field->type not set!</p>\n";
	}
return $html;
}
function captiveregistration_render_form($Q,$dblayer,$form_id,$data,$errors) {
$html = "";
$modref = "captiveregistration";
		$form = $dblayer->query("PHPcaptiveI",sprintf("select * from forms where form_id = %d",$form_id));
		if ($form->num_rows) {
			$formRow = $form->fetch_object();
			if ($formRow->form_css) {
				$html .= "<style type=\"text/css\">\n";
					$html .= $formRow->form_css."\n";
				$html .= "</style>\n";
			}
			if ($formRow->form_js) {
				$html .= "<script language=\"javascript\">\n";
					$html .= $formRow->form_js."\n";
				$html .= "</script>\n";
			}
			$html .= "<form name=\"".$formRow->form_variable."\" id=\"".$formRow->form_variable."\" method=\"post\" action=\"".$Q{'base_url'}."/?module={$modref}&task=save-registration\">\n";
			$fields = $dblayer->query("PHPcaptiveI",sprintf("select * from form_fields where form_id = %d order by o ASC",$form_id));
			if ($fields->num_rows) {
				while ($field = $fields->fetch_object()) {
					//output form fields..
					$d = null;
					if (isset($data[$field->field_variable])) {
						$d = $data[$field->field_variable];
					}
					$e = null;
					if (isset($errors[$field->field_variable])) {
						$e = $errors[$field->field_variable];
					}
					$params = array();
					if ($d) {
						$params['data'][$field->field_variable] = $d;
					}
					if ($e) {
						$params['error'][$field->field_variable] = $e;
					}
					$html .= captiveregistration_render_field($dblayer,$field,$params);
				} // while
				//output submission button
				$html .= "<input type=\"submit\" name=\"submit\" value=\"Register\" />\n";
				//hidden form variables
				$html .= "<input type=\"hidden\" name=\"form_id\" id=\"form_id\" value=\"".$formRow->form_id."\" />\n";
			}
			//form closure.
			$html .= "</form>\n";
		}
return $html;
}
function captiveregistration_render_tos($dblayer,$network) {
	$html = "";
	$tos = $dblayer->query("PHPcaptiveI",sprintf("select * from tos_pages where network_id = %d LIMIT 1",$network));
	if ($tos->num_rows) {
		$row = $tos->fetch_object();
		$html .= "<h2>".$row->title."</h2>\n";
		$html .= $row->body;
	}
	return $html;
}
function captiveregistration_theme_table_header_row($Q,$default,$input,$params) {
	$html = "";
	$trclass = "";
	$tdclass = "";
		if (count($input)) {
			if (isset($params['tr-style'])) {
				$trclass = " class='".$params['tr-style']."' ";
			}
			$html .= "<tr{$trclass}>\n";
			foreach ($input as $cell) {
				if (isset($params['td-style'])) {
					$tdclass = " class='".$params['td-style']."'";
				}
				$html .= "<td{$tdclass} valign='top'>{$cell}</td>\n";
			}
			$html .= "</tr>\n";
		}
	return $html;
}
function captiveregistration_theme_table_data_row($Q,$default,$input,$params) {
	$html = "";
	$trstyle = "";
	$tdstyle = "";
		if (count($input)) {
			if (isset($params['tr-style'])) {
				$trstyle = " class='".$params['tr-style']."'";
			}
			$html .= "<tr{$trstyle}>\n";
			foreach ($input as $cell) {
				if (isset($params['td-style'])) {
					$tdstyle = " class='".$params['td-style']."'";
				}
				$html .= "<td valign='top'{$tdstyle}>{$cell}</td>\n";
			}
			$html .= "</tr>\n";
		}
	return $html;
}
function captiveregistration_task($Q,$dblayer,$providers,$params) {
include("includes/hwaddr.php");
$page = array();
$page['title'] = "";
$page['body'] = "";
	if (isset($params['task'])) {
			$task = $params['task'];
			$modref = "captiveregistration";
			if (isset($params['modref'])) {
				$modref = $params['modref'];
			}
			switch ($task) {
			default:
				/** Specifiy the default action **/
				$page['title'] .= "Captive Registration Module";
				//$page['body'] .= "<h2>Captive Registration Module</h2>\n";
				//$page['body'] .= "<p>This page will determine if you have already registered with this site, if you have not registered you will be offered to the opportunity to do so.</p>\n";
				$hwaddr = clientHWADDR();
				//$page['body'] .= "<p>Your Ethernet Address: {$hwaddr}</p>\n";
				
				$reg = $dblayer->query("PHPcaptiveI",sprintf("select * from registrations where hwaddr = '%s'",$hwaddr));
				if ($reg->num_rows) {
					$row = $reg->fetch_object();
					$page['body'] .= "<p>This computer is registered to use the ".$row->network." Network</p>\n";
				} else {
					$page['body'] .= "<p>This computer is not registered to with the system, in order to gain internet access you will need to register.</p>\n";
					$page['body'] .= "<p><a href=\"".$Q{'base_url'}."/?module={$modref}&task=register\">Register Now</a></p>\n";
				}
				
				/** 
				$reg = new mysqldb;
				$reg->setquery($D{'dbname'},$D{'dbconn'},sprintf("select * from registrations where hwaddr = '%s'",$hwaddr));
					if ($reg->totalRows) {
						//$page['body'] .= "<p>This computer is registered to use the ".$reg->row{'network'}." Network</p>\n";
					} else {
						//$page['body'] .= "<p>You must register your computer to gain access to the internet.</p>\n";
						$network_id = captiveregistration_network_eligibility($Q,$default,$providers,$params);
						if ($network_id) {
							$network = new mysqldb;
							$network->setquery($D{'dbname'},$D{'dbconn'},sprintf("select * from networks where network_id = %d",$network_id));
							if ($network->totalRows) {
								// --> direct to the appropriate Terms & Conditions page
									$tos = new mysqldb;
									$tos->setquery($D{'dbname'},$D{'dbconn'},sprintf("select * from tos_pages where network_id = %d",$network_id));
										if ($tos->totalRows) {
											do {
												$page['body'] .= $tos->row{'body'};
											} while ($tos->row = mysql_fetch_assoc($tos->query));
										}
									$tos->disposequery();
									
								// --> complete registration form
							} else {
								$page['body'] .= "<p>There has been an error determining which Network you are eligible for.</p>\n";
							}
							$network->disposequery();
						} else {
							$page['body'] .= "<p>You are not eligible for any networks.</p>\n";
						}
						/*
						TODO:
						
						The system will have already worked out which network this user is elgible to join, the system will
						display the appropriate "You must register" message for that given network.
						
						The user will be directed to appropriate "Terms & Conditions" pages relevant to the network they are
						eligible to join.
						
						The user will eventually be asked to complete a form (if a form is set for this network) which will be
						stored in the PHPcaptives database under form_submissions.
						
						*/
					//}
					/**
				$reg->disposequery();
				 **/
				//print_r(captiveregistration_network_eligibility($Q,$default,$providers));
				//$page['body'] .= "<p>You can use this module as a template to develop your own modules!</p>\n";
				//$page['body'] .= "<h2>Credits</h2>\n";	
				//$page['body'] .= "<p>\n";
				//$page['body'] .= "Lead Developer: <a href='mailto:mike@ox-code.org'>Mike Smith</a><br />\n";
				//$page['body'] .= "Contributor: <a href='mailto:jama251@googlemail.com'>Jack Adams</a>\n";
				//$page['body'] .= "</p>\n";
				//$page['body'] .= "<h2>GPLV3 License</h2>\n<iframe src='http://www.gnu.org/licenses/gpl-3.0.txt' style='width:100%;height:400px;'></iframe>\n";
			break;
			case "page":
				//return a page from the db
				$p = 0;
				if (isset($_GET['page'])) {
					$p = GetParam("page","int","GET");
				}
				if ($p) {
					$result = $dblayer->query("PHPcaptiveI",sprintf("select * from pages where page_id = %d",$p));
					if ($result->num_rows) {
						$row = $result->fetch_object();
						$page['title'] = $row->title;
						$page['body'] = $row->body;
					} else {
						//page not found
					}
				} else {
					//page not found
				}
			break;
			case "register":
				
				if (captiveregistration_is_registered($dblayer,clientHWADDR())) {
					$page['body'] .= "<p class=\"ok\">This computer has already been registered with the system</p>\n";
				} else {
					//loookup network eligibility
					$network = captiveregistration_network_eligibility($Q,$dblayer,$providers,$modref);
					if ($network) {
						//lookup tos page for network
						$page['body'] .= captiveregistration_render_tos($dblayer,$network);
						//lookup registration form for network
						$regform = $dblayer->query("PHPcaptiveI",sprintf("select registration_form_id from networks where network_id = %d",$network));
						if ($regform->num_rows) {
							//get form fields
							$regRow = $regform->fetch_object();
							$form = $dblayer->query("PHPcaptiveI",sprintf("select * from forms where form_id = %d",$regRow->registration_form_id));
							if ($form->num_rows) {
								$formRow = $form->fetch_object();
								if ($formRow->form_css) {
									$page['body'] .= "<style type=\"text/css\">\n";
										$page['body'] .= $formRow->form_css."\n";
									$page['body'] .= "</style>\n";
								}
								if ($formRow->form_js) {
									$page['body'] .= "<script language=\"javascript\">\n";
										$page['body'] .= $formRow->form_js."\n";
									$page['body'] .= "</script>\n";
								}
								$page['body'] .= "<form name=\"".$formRow->form_variable."\" id=\"".$formRow->form_variable."\" method=\"post\" action=\"".$Q{'base_url'}."/?module={$modref}&task=save-registration\">\n";
								$fields = $dblayer->query("PHPcaptiveI",sprintf("select * from form_fields where form_id = %d order by o ASC",$regRow->registration_form_id));
								if ($fields->num_rows) {
									while ($field = $fields->fetch_object()) {
										//output form fields..
										$page['body'] .= captiveregistration_render_field($dblayer,$field);
									} // while
									//output submission button
									$page['body'] .= "<input type=\"submit\" name=\"submit\" value=\"Register\" />\n";
									//hidden form variables
									$page['body'] .= "<input type=\"hidden\" name=\"form_id\" id=\"form_id\" value=\"".$formRow->form_id."\" />\n";
								}
								//form closure.
								$page['body'] .= "</form>\n";
							}
						}
					} else {
						$page['body'] .= "<p class=\"error\">You are not entitled to register with any networks, please contact your network administrator for more information.</p>\n";
					}
				}
				/*
				$page['title'] .= "Register";
				$page['body'] .= "<h2>Please fill in the following form to complete your registration</h2>
	<p>
	Device Description:&nbsp;<input name=\"deviceDescription\" size=\"40\" type=\"text\" /><br />
	<em>Please provide a clear description of your device, for example \"HP Laptop\", \"Dell Desktop\" or \"PS3\"</em>
	</p>
<p>
	Your Antivirus Package:&nbsp;<input name=\"antivirusPackage\" size=\"40\" type=\"text\" /></p>
<p>
	<em><span style=\"font-size:10px;\">if you are using a non Windows computer you can simply place &quot;none&quot; in the box above. Windows users MUST have valid antivirus software installed.</span></em></p>
<p>
	I confirm that I will:</p>
<p>
	<input name=\"osUpdates\" type=\"checkbox\" />Keep my Operating System fully up-to-date, applying patches as soon as they become available.</p>
<p>
	<input name=\"avUpdates\" type=\"checkbox\" />Keep my Antivirus Software fully up-to-date, applying patches automatically or as soon as possible.</p>
<p>
	<input name=\"otherUpdates\" type=\"checkbox\" />I will strive to update other software I am using, such as Mozilla Firefox, Google Chrome, Adobe Flash and other popular Internet software as it becomes available.</p>";
			*/
			break;
			case "save-registration":
				//saves a posted registration form and adds client registrations to the DHCP server
				$errors = array();
				$error = array();
				$form_id = GetParam("form_id","int","POST");
				if ($form_id) {
					$form_result = $dblayer->query("PHPcaptiveI",sprintf("select * from forms where form_id = %d",$form_id));
					if ($form_result->num_rows) {
						$form = $form_result->fetch_object();
						$network_id = captiveregistration_network_eligibility($Q,$dblayer,$providers,$modref);
						$network_result = $dblayer->query("PHPcaptiveI",sprintf("select registration_form_id from networks where network_id = %d",$network_id));
						if ($network_result->num_rows) {
							$network = $network_result->fetch_object();
							if ($network_id == $network->registration_form_id) {
								//get data corresponding to form_fields
								$data = array();
								$form_field_result = $dblayer->query("PHPcaptiveI",sprintf("select * from form_fields where form_id = %d order by o ASC",$form_id));
								if ($form_field_result->num_rows) {
									while ($field = $form_field_result->fetch_object()) {
										switch ($field->type) {
										case "text":
											$data[$field->field_variable] = GetParam($field->field_variable,"text","POST");
											//check if the 'required' db field is satisfied
											if ($field->required) {
												if (!$data[$field->field_variable]) {
													$errors[] = array("field_variable" => $field->field_variable, "field_title" => $field->title, "message" => $field->required_message);
													$error[$field->field_variable] = $field->required_message;
												}
											}
											//validate the data
											if ($field->validate_id) {
												//lookup the validation routine and perform validation on the data
											}
										break;
										case "date":
											// 3 component select fields for day month and year
										break;
										case "checkbox":
											$data[$field->field_variable] = GetParam($field->field_variable,"text","POST");
											if ($field->required) {
												if ($data[$field->field_variable] == "off" || !$data[$field->field_variable]) {
													$errors[] = array("field_variable" => $field->field_variable, "field_title" => $field->title, "message" => $field->required_message);
													$error[$field->field_variable] = $field->required_message;
												}
											}
											//validate the data
											if ($field->validate_id) {
												//lookup the validation routine and perform validation on the data
											}
										break;
										case "textarea":
											$data[$field->field_variable] = GetParam($field->field_variable,"text","POST");
											if ($field->required) {
												if (!$data[$field->field_variable]) {
													$errors[] = array("field_variable" => $field->field_variable, "field_title" => $field->title, "message" => $field->required_message);
													$error[$field->field_variable] = $field->required_message;
												}
											}
											//validate the data
											if ($field->validate_id) {
												//lookup the validation routine and perform validation on the data
											}
										break;
										case "select":
											//
										break;
										}//switch
									}//while
								
									if (!count($errors)) {
										//store data and register client
										print_r($data);
									} else {
										//errors detected
										//print_r($errors);
										/**
										$page['body'] .= "<div class=\"error\">\n";
										foreach ($errors as $e) {
											$page['body'] .= "<p>".$e["message"]."</p>\n";
										}//foreach
										$page['body'] .= "</div>\n";
										**/
										//$page['body'] .= "<p class=\"error\">Please check and complete the highlighted fields below</p>\n";
											// show the registration form again and include entered data and any errors.
										//$page['body'] .= captiveregistration_render_tos($dblayer,$network_id);
										$page['body'] .= captiveregistration_render_form($Q,$dblayer,$form_id,$data,$error);
									}// if no errors else
								} else {
									$page['body'] .= "<p class=\"error\">No Fields have been defined for this Form!</p>\n";
								}
							} else {
								//wrong form for the eligible network!
								$page['body'] .= "<p class=\"error\">You have submitted the wrong registration form, please contact your network administrator!</p>\n";
							}
						} else {
							$page['body'] .= "<p class=\"error\">The network does not exist!</p>\n";
						}//if network result else
						
					}
				}//if form_id
			break;
			case "my-registrations":
				$page['title'] .= "My Registrations";
				$hwaddr = clientHWADDR();
				$T = captiveregistration_setup_custom_icons($Q);
				$Q['icons']['PHPcaptive'] = load_icon_map($T);
					$modImageSrc = "";
					if (isset($Q['icons']['PHPcaptive']['custom']['large']['PHPcaptive'])) {
						$modImageSrc = $Q['base_url']."/".$T['icons']['location']."/".$Q['icons']['PHPcaptive']['custom']['medium']['PHPcaptive'];
					} else {
						$modImageSrc = system_get_var($dblayer,"default_cpanel_icon");
					}
						$img = "<img src=\"".$modImageSrc."\" valign=\"middle\"\>";
				$page['body'] .= "<h2>{$img} My Registrations</h2>\n";
				$userInfo = current_user($dblayer);
				$un = "";
				if ($userInfo['source'] != 'local') {
					$un .= $userInfo['source']."\\";
				}
				$un .= $userInfo['username'];
				$page['body'] .= "<h3>Registrations for User : ".$userInfo['realname']." <em>{{$un}}</em></h3>\n";
				//self registration management
				$regs = $dblayer->query("PHPcaptiveI",sprintf("select * from registrations where uid = '%s' order by t DESC",$userInfo['UID']));
				if ($regs->num_rows) {
					$tableheaders = array();
  					$tableheaders[] = "Ethernet Address";
  					$tableheaders[] = "Network";
  					$tableheaders[] = "Last Seen";
  					$tableheaders[] = "Registered On";
  				$tableparams = array();
  					$tableparams['modref'] = $modref;
  					$tableparams['profile'] = "table_header_row";
  					$tableparams['tr-style'] = "table-header";
  				$themedtable['header-row'] = theme($Q,null,$tableheaders,$tableparams);
  				
  					$tableparams['profile'] = "table_data_row";
						//$tableparams['td-style'] = "data-cell";//odd or even
						$themedtable['table-data-rows'] = "";
						$oddeven = "a";
					while ($row = $regs->fetch_object()) {
						$cellData = array();
							if ($hwaddr == $row->hwaddr) {
								$current = " *";
							} else {
								$current = "";
							}
							$cellData[] = $row->hwaddr;
							$cellData[] = $row->network;
							$cellData[] = "";//lastSeen
							$cellData[] = "";//registered
							$tableparams['tr-style'] = "table-data-{$oddeven}";
							if ($oddeven == "a") {
								$oddeven = "b";
							} else {
								$oddeven = "a";
							}
							$themedtable['table-data-rows'] .= theme($Q,null,$cellData,$tableparams);
					}//while
					$page['body'] .= "<div class='data-table-wrap'><table class='data-table'>".$themedtable['header-row'].$themedtable['table-data-rows']."</table></div>\n";
				} else {
					$page['body'] .= "<p class=\"warning\">You have no registrations.</p>\n";
				}
			break;
			/**
			case "mysqli-test":
				$UID = generate_user_UID($dblayer);
				$page['body'] .= "<p>Generated UID {$UID}</p>\n";
				$result = $dblayer->query("PHPcaptiveI","select * from registrations");
				if ($result) {
					while ($row = $result->fetch_object()) {
						$page['body'] .= "<p>".$row->hwaddr."</p>\n";
					}
				}
			break;
			**/
			}//switch task
		}
return $page;
}
function captiveregistration_cpanel($Q,$dblayer,$params) {
	$modref = "captiveregistration";
	if (isset($params['modref'])) {
		$modref = $params['modref'];
	}
	$T = captiveregistration_setup_custom_icons($Q);
  	$Q['icons']['PHPcaptive'] = load_icon_map($T);
	$modImageSrc = "";
	if (isset($Q['icons']['PHPcaptive']['custom']['large']['PHPcaptive'])) {
		$modImageSrc = $Q['base_url']."/".$T['icons']['location']."/".$Q['icons']['PHPcaptive']['custom']['large']['PHPcaptive'];
	} else {
		$modImageSrc = system_get_var($dblayer,"default_cpanel_icon");
	}
	$icons = array();
		$icon = array();
			$icon['label'] = "Network Registration";
			$icon['link'] = $Q{'base_url'}."/?module={$modref}";
			$icon['icon'] = $modImageSrc;
			$icon['group'] = "General";
		$icons[] = $icon;
	return $icons;
}
function captiveregistration_local_tasks($Q,$dblayer,$providers,$params) {
$icons = array();

	$modref = "captiveregistration";
	if (isset($params['modref'])) {
		$modref = $params['modref'];
	}
	$T = captiveregistration_setup_custom_icons($Q);
  	$Q['icons']['PHPcaptive'] = load_icon_map($T);
	$modImageSrc = "";
	if (isset($Q['icons']['PHPcaptive']['custom']['large']['PHPcaptive'])) {
		$modImageSrc = $Q['base_url']."/".$T['icons']['location']."/".$Q['icons']['PHPcaptive']['custom']['medium']['PHPcaptive'];
	} else {
		$modImageSrc = system_get_var($dblayer,"default_cpanel_icon");
	}
		$icon = array();
			$icon['label'] = "Register";
			$icon['link'] = $Q{'base_url'}."/?module={$modref}&task=register";
			$icon['icon'] = $modImageSrc;
			$icon['group'] = "Network";//probably will not be used
		$icons[] = $icon;
		$icon['label'] = "Registrations";
			$icon['link'] = $Q{'base_url'}."/?module={$modref}&task=my-registrations";
			$icon['icon'] = $modImageSrc;
			$icon['group'] = "Network";//probably will not be used
		$icons[] = $icon;
		if (isset($params['task'])) {
			switch ($params['task']) {
			default:
			
			break;
			case "register":
				//show pages relevant to a given network
				$network = captiveregistration_network_eligibility($Q,$dblayer,$providers,$modref);
				$result = $dblayer->query("PHPcaptiveI",sprintf("select np.nrpid,np.network_id,p.page_id,p.title,p.body from network_related_pages np LEFT JOIN pages p ON np.page_id = p.page_id WHERE np.network_id = %d",$network));
				if ($result->num_rows) {
						$icon['label'] = "Related Pages";
						$icon['link'] = null;
						$icon['icon'] = null;
						$icon['group'] = null;
					$icons[] = $icon; 
					while ($page = $result->fetch_object()) {
						$icon['label'] = $page->title;
						$icon['link'] = $Q{'base_url'}."/?module={$modref}&task=page&page=".$page->page_id."&network=".$network;
						$icon['icon'] = $modImageSrc;
						$icon['group'] = "Network";//probably will not be used
					$icons[] = $icon;
					}//while
				}
			break;
			case "page":
				if (isset($_GET['network'])) {
					$network = GetParam("network","int","GET");
					$result = $dblayer->query("PHPcaptiveI",sprintf("select np.nrpid,np.network_id,p.page_id,p.title,p.body from network_related_pages np LEFT JOIN pages p ON np.page_id = p.page_id WHERE np.network_id = %d",$network));
						if ($result->num_rows) {
								$icon['label'] = "Related Pages";
								$icon['link'] = null;
								$icon['icon'] = null;
								$icon['group'] = null;
							$icons[] = $icon; 
							while ($page = $result->fetch_object()) {
								$icon['label'] = $page->title;
								$icon['link'] = $Q{'base_url'}."/?module={$modref}&task=page&page=".$page->page_id."&network=".$network;
								$icon['icon'] = $modImageSrc;
								$icon['group'] = "Network";//probably will not be used
							$icons[] = $icon;
							}//while
						} // if result num_rows
				} // if isset network
			break;
			}//switch
		}
		
		/**
		$icon['label'] = "MySQLi Test";
			$icon['link'] = $Q{'base_url'}."/?module={$modref}&task=mysqli-test";
			$icon['icon'] = $modImageSrc;
			$icon['group'] = "Network";//probably will not be used
		$icons[] = $icon;
		**/
return $icons;
}
?>
